package com.nstar.splus.model
{
	import com.nstar.base.util.StringUtils;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.utils.StringUtil;
	
	/**
	组件名称：新浪微博首页HTML解析类
	组件说明：获取新浪微博首页的HTML内容，并解析其中的热门话题、有趣的人等信息
	开发时间：2010.08.27
	开发人员: Jonny
	*/
	public class SinaTParser extends EventDispatcher
	{
		private var _url:String = "http://t.sina.com.cn";
		
		private var urlLoader:URLLoader;
		private var _html:String;
		
		private var _hotTopics:Array;
		private var _usedPeople:Array;
		private var _funnyPeople:Array;
		private var _top10:Array;
		
		private var parseCompleted:Boolean = false;
		
		public function SinaTParser()
		{
			
		}
		
		public function load(url:String=null):void{
			parseCompleted = false;
			if(url){
				this._url = url;
			}
			
			if(!urlLoader){
				urlLoader = new URLLoader();
			}
			
			urlLoader.load(new URLRequest(_url));
			if(urlLoader.hasEventListener(Event.COMPLETE)==false){
				urlLoader.addEventListener(Event.COMPLETE,urlLoaderHandler);
				urlLoader.addEventListener(IOErrorEvent.IO_ERROR,urlLoaderHandler);
			}
		}
		
		private function urlLoaderHandler(event:Event):void{
			switch(event.type){
				case Event.COMPLETE:
					_html = String(event.target.data);
					
					//解析HTML页面，获取数据
					_hotTopics = getHotTopics();
					_usedPeople = getUsedPeople();
					_funnyPeople = getFunnyPeople();
					_top10 = getTop10();
					
					break;
				case IOErrorEvent.IO_ERROR:
					trace("load "+_url+" error!");
					break;
			}
			this.dispatchEvent(new Event(Event.COMPLETE));
			parseCompleted = true;
		}
		
		//获取最热门的话题
		public function getHotTopics():Array{
			var result:Array = new Array();
			var article:String = "<ul class=\"acticle_list\" id=\"acticle_list\">";
			var idxArticleStart:int = _html.indexOf(article);
			if(idxArticleStart!=-1){
				var idxArticleEnd:int = _html.indexOf("</ul>",idxArticleStart);
				if(idxArticleEnd!=-1){
					var topic:String = _html.substring(idxArticleStart+article.length,idxArticleEnd);
					var e:String = "<a target=\"_blank\"";
					if(topic.indexOf(e)!=-1){//出现多余的属性，替换掉
						topic = StringUtils.replace(topic,e,"<a ");
					}
					
					var xml:XML = new XML("<root>"+StringUtil.trim(topic)+"</root>");
					var topicList:XMLList = xml.li;
					for each(var t:XML in topicList){
						var attrProfileUrl:XMLList = t.a[0].attribute("href");
						var topicVO:TopicVO = new TopicVO();
						topicVO.topic = t.a;
						topicVO.topicUrl = _url+attrProfileUrl;
						if(TopicVO.exists(topicVO,result)==false){
							result.push(topicVO);
						}
					}
				}
			}
			return result;
		}
		
		//获取有趄的人或哪些人正在用微博
		private function getPeople(startIndex:int):Array{
			var result:Array = new Array();
			var user:String = "<ul class=\"userlist\">";
			var idxUserStart:int = _html.indexOf(user,startIndex);
			if(idxUserStart!=-1){
				var idxUserEnd:int = _html.indexOf("</ul>",idxUserStart);
				if(idxUserEnd!=-1){
					var user:String = _html.substring(idxUserStart+user.length,idxUserEnd);
					var xml:XML = new XML("<root>"+StringUtil.trim(user)+"</root>");
					var userList:XMLList = xml.li;
					for each(var u:XML in userList){
						var userVO:WebUserVO = new WebUserVO();
						var attrProfileUrl:XMLList = u.a[0].attribute("href");
						var attrProfileImgSrc:XMLList = u.a[0].img.attribute("dynamic-src");
						var attrDesc:XMLList = u.a[0].img.attribute("title");
						var attrUID:XMLList = u.a[0].img.attribute("uid");
						var attrName:XMLList = u.a[1].attribute("title");
						
						userVO.uid = attrUID;
						userVO.name = attrName;
						userVO.desc = attrDesc;
						userVO.userUrl = attrProfileUrl;
						userVO.profileImgUrl = attrProfileImgSrc;
						result.push(userVO);
					}
				}
			}
			return result;
		}
		
		//获取正在使用微博的人
		public function getUsedPeople():Array{
			var index:int = _html.indexOf("这些人正在用微博");
			if(index==-1){
				index = 0;
			}
			return getPeople(index);
		}

		//获取有趣的人
		public function getFunnyPeople():Array{
			var index:int = _html.indexOf("有趣的人");
			if(index==-1){
				index = 0;
			}			
			return getPeople(index);
		}
		
		//获取微博Top10使用者
		public function getTop10():Array{
			var result:Array = new Array();
			var user:String = "<ul class=\"tLoginTop10List\">";
			var idxUserStart:int = _html.indexOf(user);
			if(idxUserStart!=-1){
				var idxUserEnd:int = _html.indexOf("</ul>",idxUserStart);
				if(idxUserEnd!=-1){
					var user:String = _html.substring(idxUserStart+user.length,idxUserEnd);
					var xml:XML = new XML("<root>"+StringUtil.trim(user)+"</root>");
					var userList:XMLList = xml.li;
					var seq:int = 1;
					for each(var u:XML in userList){
						var userVO:Top10UserVO = new Top10UserVO();
						userVO.seq = seq;
						var attrProfileUrl:XMLList = u.span[0].a[0].attribute("href");
						var attrUID:XMLList = u.span[0].a[0].attribute("uid");
						var attrName:XMLList = u.span[0].a[0].attribute("title");
						var cite:XMLList = u.cite;
						userVO.uid = attrUID;
						userVO.userUrl = attrProfileUrl;
						userVO.name = attrName;
						userVO.followers = cite;
						result.push(userVO);
						seq++;
					}
				}
			}
			return result;
		}
		
		public function get hotTopics():Array{
			return _hotTopics;
		}		

		public function get usedPeople():Array{
			return _usedPeople;
		}		

		public function get funnyPeople():Array{
			return _funnyPeople;
		}		

		public function get top10():Array{
			return _top10;
		}		
	}
}